<template>
  <div class="bottom-menu">
    <check-button
      class="select-all"
      @checkBtnClick="checkBtnClick"
      v-model="isSelectAll"
    ></check-button>

    <span class="all">全选</span>
    <span class="total-price">合计：￥{{ totalPrice }}</span>
    <span class="buy-product" @click="goBuy"
      >去结算：({{ $store.getters.cartLength }})</span
    >
  </div>
</template>

<script>
import CheckButton from "./CheckButton";
export default {
  name: "BottomBar",
  components: {
    CheckButton,
  },
  computed: {
    totalPrice() {
      const cartList = this.$store.getters.cartList;
      return cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return preValue + item.count * item.price;
        }, 0)
        .toFixed(2);
    },
    // cartCount() {
    //   return this.$store.getters.cartList.filter((item) => item.checked.length);
    // },
    isSelectAll: function () {
      return (
        this.$store.getters.cartList.find((item) => item.checked === false) ===
        undefined
      );
    },
  },
  methods: {
    checkBtnClick: function () {
      // 1.判断是都有未选中的按钮
      let isSelectAll = this.$store.getters.cartList.find(
        (item) => !item.checked
      );
      // 2.如果有未选中的内容，则全部选中
      if (isSelectAll) {
        this.$store.state.cartList.forEach((item) => {
          item.checked = true;
        });
      } else {
        this.$store.state.cartList.forEach((item) => {
          item.cartList = false;
        });
      }
    },
    goBuy() {
      this.$router("");
    },
  },
};
</script>

<style scoped>
.bottom-menu {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 50px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}

.bottom-menu .select-all {
  padding: absolute;
  line-height: 0;
  left: 12px;
  padding-top: 13px;
}

.bottom-menu .all {
  position: fixed;
  left: 55px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  background-color: var(--color-tint);
  color: #fff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  float: right;
}
</style>
